package com.clj.view;

import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.Spring;
import javax.swing.SpringLayout;

import com.clj.handler.LoginHandler_clj;

/**
 * Author: Harry Chen
 * Date: 2023/02/24 15:52
 * Description: 登录页
 */
public class LoginView_clj extends JFrame {
    JLabel nameLabel = new JLabel("学生成绩管理系统 for MySql", JLabel.CENTER);

    SpringLayout springLayout = new SpringLayout();
    JPanel centerPanle = new JPanel(springLayout);
    JLabel userNameLable = new JLabel("用户名:");
    JTextField userTxt = new JTextField();
    JLabel pwdLabel = new JLabel("密码:");
    JPasswordField pwdField = new JPasswordField();
    JButton loginBtn = new JButton("登录");
    JButton resetBtn = new JButton("重置");

    //托盘
    SystemTray systemTray;
    TrayIcon trayIcon;
    LoginHandler_clj loginHandlerClj;

    public LoginView_clj() {
        Container contentPane = getContentPane();


        nameLabel.setFont(new Font("华文行楷", Font.PLAIN, 40));
        nameLabel.setPreferredSize(new Dimension(0, 80));
        userNameLable.setFont(new Font("楷体", Font.PLAIN, 20));
        userTxt.setPreferredSize(new Dimension(200, 30));
        pwdLabel.setFont(new Font("楷体", Font.PLAIN, 20));
        pwdField.setPreferredSize(new Dimension(200, 30));
        loginBtn.setFont(new Font("楷体", Font.PLAIN, 20));
        resetBtn.setFont(new Font("楷体", Font.PLAIN, 20));
        centerPanle.add(userNameLable);//把组件加入面板
        centerPanle.add(userTxt);
        centerPanle.add(pwdLabel);
        centerPanle.add(pwdField);

        //有问题在这里
        loginHandlerClj = new LoginHandler_clj(this);
        loginBtn.addActionListener(loginHandlerClj);
        loginBtn.addKeyListener(loginHandlerClj);
        centerPanle.add(loginBtn);
        resetBtn.addActionListener(loginHandlerClj);
        centerPanle.add(resetBtn);


        Spring childwidth = Spring.sum(Spring.sum(Spring.width(userNameLable), Spring.width(userTxt)), Spring.constant(20));
        int offsetX = childwidth.getValue() / 2;
        springLayout.putConstraint(SpringLayout.WEST, userNameLable, -offsetX,
                SpringLayout.HORIZONTAL_CENTER, centerPanle);
        springLayout.putConstraint(SpringLayout.NORTH, userNameLable, 20,
                SpringLayout.NORTH, centerPanle);

        springLayout.putConstraint(SpringLayout.WEST, userTxt, 20,
                SpringLayout.EAST, userNameLable);
        springLayout.putConstraint(SpringLayout.NORTH, userTxt, 0,
                SpringLayout.NORTH, userNameLable);

        springLayout.putConstraint(SpringLayout.EAST, pwdLabel, 0,
                SpringLayout.EAST, userNameLable);
        springLayout.putConstraint(SpringLayout.NORTH, pwdLabel, 20,
                SpringLayout.SOUTH, userNameLable);

        springLayout.putConstraint(SpringLayout.WEST, pwdField, 20,
                SpringLayout.EAST, pwdLabel);
        springLayout.putConstraint(SpringLayout.NORTH, pwdField, 0,
                SpringLayout.NORTH, pwdLabel);

        springLayout.putConstraint(SpringLayout.WEST, loginBtn, 50,
                SpringLayout.WEST, pwdLabel);
        springLayout.putConstraint(SpringLayout.NORTH, loginBtn, 20,
                SpringLayout.SOUTH, pwdLabel);

        springLayout.putConstraint(SpringLayout.WEST, resetBtn, 50,
                SpringLayout.EAST, loginBtn);
        springLayout.putConstraint(SpringLayout.NORTH, resetBtn, 0,
                SpringLayout.NORTH, loginBtn);

        contentPane.add(nameLabel, BorderLayout.NORTH);
        contentPane.add(centerPanle, BorderLayout.CENTER);

        if (SystemTray.isSupported()) {
            systemTray = SystemTray.getSystemTray();
            trayIcon = new TrayIcon(new ImageIcon("picture\\clj.jpg").getImage());
            trayIcon.setImageAutoSize(true);//图片大小与托盘大小不适配，设置托盘图片大小自动缩放
            try {
                systemTray.add(trayIcon);
            } catch (AWTException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //增加最小化时销毁资源
            this.addWindowListener(new WindowAdapter() {
                public void windowIconified(WindowEvent e) {
                    LoginView_clj.this.dispose();
                }
            });
            trayIcon.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    int clickCount = e.getClickCount();
                    if (clickCount == 1) {
                        LoginView_clj.this.setExtendedState(JFrame.NORMAL);
                    }
                    LoginView_clj.this.setVisible(true);
                }
            });
        }


        setTitle("陈林杰-成绩管理系统 for MySql");
        getRootPane().setDefaultButton(loginBtn);
        //loginHandler =new LoginHandler(this);
        URL imgUrl = LoginView_clj.class.getClassLoader().getResource("clj.jpg");
        ImageIcon icon = new ImageIcon(new ImageIcon(imgUrl).getImage());
        setIconImage(icon.getImage()); //给窗体设置图标方法
        setSize(600, 400);
        setVisible(true);
        setLocationRelativeTo(null);//居中
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭退出程序
        setResizable(false);//大小可改变
    }

    public static void main(String[] args) {
        new LoginView_clj(); // TODO Auto-generated method stub

    }

    public JTextField getUserTxt() {
        return userTxt;
    }

    public JPasswordField getPwdField() {
        return pwdField;
    }
}
